package com.wgz.mcp;

import com.wgz.mcp.service.KnowledgeBaseService;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.tool.method.MethodToolCallbackProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@MapperScan(basePackages = "com.wgz.mcp.mapper")
public class McpServerApplication {

    private static final Logger log = LoggerFactory.getLogger(McpServerApplication.class);

    public static void main(String[] args) {
        // 在 Spring 启动前设置系统属性，确保无输出
        System.setProperty("spring.main.banner-mode", "off");
        System.setProperty("logging.level.root", "ERROR");

        try {
            // 静默启动，不输出任何信息到 stdio
            SpringApplication app = new SpringApplication(McpServerApplication.class);
            app.setLogStartupInfo(false);  // 关键：禁用启动信息
            app.setBannerMode(org.springframework.boot.Banner.Mode.OFF);
            app.run(args);
        } catch (Exception e) {
            // 错误信息输出到 stderr，而不是 stdout
            System.err.println("MCP Server 启动失败: " + e.getMessage());
            System.exit(1);
        }
    }

    @Bean
    public ToolCallbackProvider testTools(KnowledgeBaseService knowledgeBaseService) {
        return MethodToolCallbackProvider.builder()
                //注册mcp server
                .toolObjects(knowledgeBaseService)
                .build();
    }
}